<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

Class DiscountController Extends BaseController
{

	private $page = 1;
	//private $numPageShow = '';
	
    public function index($page = 1) {  
  	//$db = new DB();	
        $this->page = $page;  	    	  	            	     	 
      	
      	//get book number of one special page  	
      	$num = $this->registry->db->get('configManager')->getById(1)->get("value"); //get special page size
      
      	$showedBooks = $this->registry->db->get('bookManager')->getSpecialBooks(($this->page-1)*$num, $num);   	 	 	 
      	$numOfSpecial = $this->registry->db->get('bookManager')->getNumberOfSpecialBook();
      	$specialBooksContent = '
            <div class="title"><span class="title_icon"><img src="{bstoViewPath}images/bullet1.gif" alt="" title="" /></span>Discount books</div><div class="new_products">';
        //$specialBooksContent = $specialBooksContent.$this->registry->template->getSpecialBooksContent($showedBooks, $this->showPagingBar($numOfSpecial, $num));
      	 $specialBooksContent .= '</div>'; 	
      	//get category & about our store		    
        $aboutOurStore = array_pop($this->registry->db->get('staticManager')->getAboutOurStore());           	   
        $cat = $this->registry->template->getCatContent($this->registry->db->get('catManager')->getCategory());
        $tags = array('bstoAboutContent' => $aboutOurStore->get('detail'),
                      'bstoCatContent' => $cat);            
        
        $this->registry->template->setMenuKey('special');
    	$user = $this->registry->db->get('userManager')->getCurrentUser();
    	$tags = array('bstoHead'=>"");
        if($user != null){
        	$tags['login'] = '<li id="login"><span> Hello, '. $user->get('username').'</span><a href="{bstoRootPath}logout">Logout</a></li>';	
        }else{
        	$tags['login'] = '<li id="login"><a href="{bstoRootPath}login">Login</a></li>';
        }
        $output = $this->registry->template->getPageContent('index', 'Discount Books', $specialBooksContent, $tags);    				                     
        echo $output;      
    }   
  
    public function showPagingBar( $numOfBook, $num){
        $numOfPage = ceil($numOfBook / $num);
        $str = '';
        if($numOfBook == 0 || $numOfPage == 1){
            $str .= '<div class="pagination"> </div>';
        }else {
            if($numOfPage == 1){
                $str ='<div class="pagination"> <span class="disabled">[First]</span> ';
                $str .='<span class="disabled">&lt;&lt;</span> ';
                $str .= '<a href="{bstoRootPath}special/1"><span class="current">1</span></a>';
                $str .= '<span class="disabled"> &gt;&gt; </span>';
                $str .= '<span class="disabled"> [Last] </span>';
                $str .= '</div>';
            }else if ($this->page == 1){
                $str ='<div class="pagination"> <span class="disabled">[First]</span> ';
                $str .='<span class="disabled">&lt;&lt;</span> ';
                $i = 0;
                for($i=$this->page ; $i < $this->page + 3; $i++){
                    if($i <= $numOfPage){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}special/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}special/' .$i. '">' .$i. '</a>';    
                        }
                            
                    }            
                }
                if($i < $numOfPage){
                    $str .= "...";
                }
                $next = $this->page + 1;
                $str .= '<a href="{bstoRootPath}special/' .$next . '"> &gt;&gt; </a>';
                $str .= '<a href="{bstoRootPath}special/' .$numOfPage . '"> [Last] </a>';
                $str .= '</div>';
        		            
            } else if ($this->page == $numOfPage){
                $previous = $this->page - 1;
                $str ='<div class="pagination"> <a href="{bstoRootPath}special/1">[First]</a>';
                $str .='<a href="{bstoRootPath}special/' .$previous.'">&lt;&lt;</a>';
                $i = $this->page - 2;
                if($i > 1){
                    $str .= "...";
                }
                for($i=$this->page - 2; $i <= $numOfPage; $i++){
                    if($i >= 1){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}special/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}special/' .$i. '">' .$i. '</a>';    
                        }                    
                    }            
                }    
                $str .= '<span class="disabled"> &gt;&gt; </span>';
                $str .= '<span class="disabled"> [Last] </span>';
                $str .= '</div>';
                       
            } else {
                $previous = $this->page - 1;
                $next = $this->page + 1;
                $str .='<div class="pagination"> <a href="{bstoRootPath}special/1">[First]</a>';
                $str .='<a href="{bstoRootPath}special/' .$previous. '">&lt;&lt;</a>';
                $i = $this->page;
                for($i = $this->page -1 ; $i < $this->page + 2; $i++){
                    if($i <= $numOfPage){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}special/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}special/' .$i. '">' .$i. '</a>';    
                        }    
                    }                    
                }
                if($i < $numOfPage){
                    $str .= "...";
                }
                $str .= '<a href="{bstoRootPath}special/' .$next. '"> &gt;&gt; </a>';
                $str .= '<a href="{bstoRootPath}special/' .$numOfPage . '"> [Last] </a>';
                $str .= '</div>'; 
                     
            }
        }
        
        return $str;   
   }
  }
?>